﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace DotNetX.UI.Web
{
    /// <summary>
    /// This provides basic general functionality to a page
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// The current language of the site
        /// </summary>
        public Language CurrentLanguage
        {
            get
            {
                // do we have a language at the moment?
                if (this.Session["language"] == null)
                {
                    // set the default language
                    this.Session["language"] = Languages.DefaultLanguage;
                }

                // return the language
                return this.Session["language"] as Language;
            }
            set
            {
                // set the language
                this.Session["language"] = value;

                // translate this page to the new language
                this.TranslateWholePage();
            }
        }

        /// <summary>
        /// Is the derieved page translatable?
        /// </summary>
        public bool IsTranslatable
        {
            get
            {
                // return the value
                return this._isTranslatable;
            }
            set
            {
                // set the value
                this._isTranslatable = value;
            }
        }
        private bool _isTranslatable = false;

        /// <summary>
        /// If set to true, this page will not load unless a the user is authenticated
        /// </summary>
        /// <remarks>
        /// This value is best set in the class constructor of the page - this way the value is set before any of the events (PageInit, PageLoad etc...) are fired
        /// </remarks>
        private bool RequiresAuthentication
        {
            get
            {
                // return the value
                return this._requiresAuthentication;
            }
            set
            {
                // set the value
                this._requiresAuthentication = value;
            }
        }
        private bool _requiresAuthentication = false;

        /// <summary>
        /// When the page inits
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            // do base logic
            base.OnInit(e);

            // does this page require authentication?
            if (this.RequiresAuthentication)
            {
                // is the user logged on?
                // TODO: Create an authentication interface, and a class that implements it that uses ASP .NET authentication and use this class for all authentication needs
                // like to see if the user is logged on
                // then direct the user, if not logged on, to WebSettings.AuthenticationPagePath
            }

            // shall we translate this page?
            if (this.IsTranslatable)
            {
                // translate this page
                this.TranslateWholePage();
            }
        }

        /// <summary>
        /// This translates the whole page starting from the controls this page has
        /// </summary>
        public void TranslateWholePage()
        {
            // translate this page
            this.TranslatePage(this.Controls);
        }

        /// <summary>
        /// This translates this page
        /// </summary>
        /// <param name="controls">The controls to translate</param>
        private void TranslatePage(ControlCollection controls)
        {
            if (controls == null)
                throw new ArgumentNullException("'controls' is null.");

            // do we have any controls to translate?
            if (controls.Count == 0)
                return;

            // walk every control to find a translatable one
            foreach (Control control in controls)
            {
                // is this control translatable?
                ITranslatableControl translatableControl = control as ITranslatableControl;
                if (translatableControl != null)
                {
                    // translate this control
                    translatableControl.Translate(this.CurrentLanguage);
                }

                // translate the children controls
                this.TranslatePage(control.Controls);
            }
        }
    }
}
